<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加服务</title>
    <style>
        .wu-pos-a {
            box-sizing: border-box;
            padding-top: 39px;
            width: 20%;
            left: auto;
            right: 0;
        }
        .wu-m5>input{
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
<div class="pos-r">
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span>
        商户管理
        <span class="c-gray en">&gt;</span>
        商户信息
        <span class="c-gray en">&gt;</span>
        添加商户
    </nav>
    <!--<div class="pos-a wu-pos-a">-->
        <!--<div class="wu-tree">商户可服务类型</div>-->
        <!--<div class="wu-treeBox">-->
            <!--<ul id="merchantAddTree" class="ztree"></ul>-->
        <!--</div>-->

    <!--</div>-->

    <!--<div style="margin-right:22%">-->
    <div>
        <div class="page-container">
            <form action="" method="post" class="form form-horizontal" id="form-member-add">
                <div class="form-unit">商户信息</div>
                <input style="display: none;" id="merchantArray" name="array"/>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户名称：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" value="" placeholder="必填项" id="bizName" name="bizName">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户状态：</label>
                    <div class="formControls col-xs-4 col-sm-4 skin-minimal">
                        <div class="radio-box">
                            <input type="radio" id="state-1" name="bizState" value="0" checked>
                            <label for="state-1">开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="radio-box">
                            <input type="radio" id="state-2" name="bizState" value="1">
                            <label for="state-2">禁用</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>联系电话：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" value="" placeholder="必填项" id="phone" name="phone">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户账号：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="必填项" name="userName" id="userName" value="">
                    </div>
                    <span id="usernameSpan"></span>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户密码：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="password" class="form-control" placeholder="必填项" name="password" id="password">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>角色：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="" name="roleId" id="roleId">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>联系地址：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="" name="address" id="address">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input id="merchantAddBut" onclick="platformMerchantMerchantAddFun.submitAction();return false;" class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    //商户管理，添加服务
    var platformMerchantMerchantAddFun;

    platformMerchantMerchantAddFun = {
        init: function () {
            let _this = this;
//            _this.serviceType();
            _this.bootstrapvalidatorForm();
            //加载人名
            $.ajax({
                type: "GET",
                url: "biz/getBizUser",
                dataType: "json",
                success: function (data) {
                    $("#sysUserId").select2({
                        data: data,
                        placeholder: '请选择',
                        allowClear: true
                    })

                },
                error: function () {
                    alert("error");
                }
            });
            //加载角色
            $.ajax({
                type: "GET",
                url: "menu/getBizRoles",
                dataType: "json",
                success: function (data) {
                    $("#roleId").select2({
                        data: data,
                        placeholder: '请选择'
                    });
                    $('#roleId').on('select2:select', function (e) {
                        $(e.target).siblings('I').removeClass('glyphicon glyphicon-remove').addClass('glyphicon glyphicon-ok').show().siblings('SMALL').hide().parents('.form-group').removeClass('has-error').addClass('has-success')
                    });
                    $('#roleId').on('select2:unselect', function (e) {
                        $(e.target).siblings('I').removeClass('glyphicon glyphicon-ok').addClass('glyphicon glyphicon-remove').show().siblings('SMALL').show().parents('.form-group').removeClass('has-success').addClass('has-error')
                    });
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //验证用户名是否存在
        usernameCheck: function (par) {
            $.ajax({
                type: "GET",
                url: "biz/checkTenantName",
                data: {userName: par},
                dataType: "json",
                success: function (data) {
                    $("#usernameSpan").text(data.message);
                    if (data.state) {
                        $("#usernameSpan").css("color", "green");
                    } else {
                        $("#usernameSpan").css("color", "red");
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //表单提交
        submitAction: function () {
            //bootstrap验证
            var bootstrapValidator = $("#form-member-add").data('bootstrapValidator');
            bootstrapValidator.validate();
            var roleId = $("#roleId").select2('val');  //获取selectid
            if(bootstrapValidator.isValid()&&!(roleId == null || roleId == undefined || roleId == "")){
                /*验证通过且roleId不为空*/
                $("#form-member-add").submit();
            } else if(!bootstrapValidator.isValid()){
                /*验证通过，不判断是否为空*/
                return;
            }else if(roleId == null || roleId == undefined || roleId == ""){
                layer.alert("角色不能为空！", {icon: 0});
               return;
            }else {return;}

//            var zTreeTemp = $.fn.zTree.getZTreeObj("merchantAddTree");
//            var nodesTemp = zTreeTemp.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
//            if(nodesTemp.length == 0){
//                layer.alert("请选择至少一个服务项目！", {icon: 0});
//                return false;
//            }

            //验证
//            //负责人
//            var sysUserId = $("#sysUserId").select2('val');  //获取selectid
//            if (sysUserId == null || sysUserId == undefined || sysUserId == "") {
//                layer.alert("负责人不能为空！", {icon: 0});
//                return false;
//            }
//
//            var treeObj = $.fn.zTree.getZTreeObj("merchantAddTree");
//            var nodes = treeObj.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
//            var array = new Array();
//            for (var i = 0; i < nodes.length; i++) {
//                if($("#wu"+nodes[i].id).val() == ""){
//                    array.push(0);
//                    continue;
//                }
//                array.push(nodes[i].id);
//                array.push($("#wu"+nodes[i].id).val()*100);
//            }
//            $("#merchantArray").val(array);

            //提交
            $.ajax({
                type: "POST",
                url: "biz/addBizTenant",
                data: $("#form-member-add").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.state) {
                        layer.msg(data.message, {icon: 1});

                        //关闭新增窗口
                        removeIframe();
                        platformMerchantMerchantFun.searchAction();
                    } else {
                        $("#usernameSpan").text(data.message);
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //加载商户可服务类型
//        serviceType: function () {
//            $.ajax({
//                type: "get",
//                url: "biz/configureServiceType",
//                dataType: "json",
//                success: function (data) {
//                    var merchantAddSetting = {
//                        check: {
//                            enable: true
//                        },
//                        data: {
//                            simpleData: {
//                                enable: true
//                            }
//                        },
//                        callback : {
//                            onCheck : function (event, treeId, treeNode) {
//                                switch(treeNode.id)
//                                {
//                                    case '1':
//                                        treeNode.checked && ($("#wu1").attr("disabled",false).focus()) || (!treeNode.checked) && ($("#wu1").attr("disabled",true).val(""));
//                                        break;
//                                    case '2':
//                                        treeNode.checked && ($("#wu2").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wu2").attr("disabled",true).val(""));
//                                        break;
//                                    case '3' :
//                                        treeNode.checked && ($("#wu3").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wu3").attr("disabled",true).val(""));
//                                        break;
//                                    case '4' :
//                                        treeNode.checked && ($("#wu4").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wu4").attr("disabled",true).val(""));
//                                        break;
//                                    default:
//                                        break;
//                                }
//                            }
//                        }
//                    };
//                    $.fn.zTree.init($("#merchantAddTree"), merchantAddSetting, data);
//                    var treeObj = $.fn.zTree.getZTreeObj("merchantAddTree");
//                    treeObj.expandAll(true);
//                    //父节点不能选择
//                    var nodes = treeObj.transformToArray(treeObj.getNodes());
//                    for (var i = 0, l = nodes.length; i < l; i++) {
//                        if (nodes[i].isParent) {
//                            treeObj.setChkDisabled(nodes[i], true);
//                        }
//                    }
//                },
//                error: function () {
//                    alert("error");
//                }
//            });
//        },
        //bootstrap验证
        bootstrapvalidatorForm: function () {
            $('#form-member-add').bootstrapValidator({
                message: 'This value is not valid',
                excluded : [':disabled'],
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    bizName: {
                        validators: {
                            notEmpty: {
                                message: '商户名称不能为空'
                            },
                            stringLength: {
                                min: 2,
                                max: 100,
                                message: '商户名称长度必须在2到100位之间'
                            }
                        }
                    },
                    phone: {
                        validators: {
                            notEmpty: {
                                message: '联系电话不能为空'
                            },
                            regexp: {
                                regexp: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/,
                                message: '联系电话格式不正确'
                            }
                        }
                    },
                    userName: {
                        validators: {
                            notEmpty: {
                                message: '商户账号不能为空'
                            },
                            stringLength: {
                                min: 4,
                                max: 50,
                                message: '商户账号长度必须在4到50位之间'
                            },
                            threshold:4,
                            remote:{
                                url:"biz/checkTenantName",
                                message: "该商户账号已存在",
                                delay: 2000,
                                type:"GET"
                            }
                        }
                    },
                    password: {
                        validators: {
                            notEmpty: {
                                message: '商户密码不能为空'
                            },
                            stringLength: {
                                min: 6,
                                max: 32,
                                message: '商户密码长度必须在6到32位之间'
                            }
                        }
                    },
                    wu1: {
                        validators: {
                            notEmpty: {
                                message: '洗车提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '洗车提成格式不正确'
                            }
                        }
                    },
                    wu2: {
                        validators: {
                            notEmpty: {
                                message: '美容提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '美容提成格式不正确'
                            }
                        }
                    },
                    wu3: {
                        validators: {
                            notEmpty: {
                                message: '保养提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '保养提成格式不正确'
                            }
                        }
                    },
                    wu4: {
                        validators: {
                            notEmpty: {
                                message: '维修提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '维修提成格式不正确'
                            }
                        }
                    }
                   /* roleId: {
                        validators: {
                            notEmpty: {
                                message: '角色不能为空'
                            }
                        }
                    }*/
                }
            });
        }
    };

    platformMerchantMerchantAddFun.init();

</script>
</body>
</html>